<?php
global $Config;
require_once($Config->WebCMSRoot.'Base/View.php');

Class BlogView extends BaseView
{
	function BlogView()
	{
		parent::BaseView();
	}
	
	function PrintIndex(&$posts, $error)
	{
		// TODO: Fix HTML in content
		if ($error[0])
		{
			$this->PrintError($error);
		}
		else
		{
			$this->output = '';

			$posts = str_replace("\n", '', $posts);
			$matches = preg_match_all('|.*<entry>.*<id>(?P<id>.*)</id>.*<published>(?P<published>.*)</published>.*<title[^>]*>(?P<title>.*)</title>.*<content[^>]*>(?P<content>.*)</content>.*</entry>.*|', $posts, $blog, PREG_SET_ORDER);

			foreach ($blog as $key=>$post)
			{
				preg_match_all('|.*full/(?P<id>\d*)|', $post['id'], $id, PREG_SET_ORDER);
				
				
				$this->output .= '<h3>' . htmlspecialchars($post['title']) . '</h3>';
				$this->output .= '<span style="font-style:italic;font-size:80%;">' . $post['published'] . ':</span>';
				$this->output .= '<p>';
				$this->output .= substr($post['content'], 0, 300) . '...';
				$this->output .= '</p>';
				$this->output .= 'Lees <a href="/blog/'.$id[0]['id'].'">' . htmlspecialchars($post['title']) . '</a> ';
				$this->output .= '<br />';
			}
		}
	}
	
	function PrintSummaries(&$blog, $error)
	{
		if ($error[0])
		{
			$this->PrintError($error);
		}
		else
		{
			$blog = preg_replace("|\s*xmlns=['\"][^'\"]*['\"]|", "", $blog);
//		$blog = utf8_encode($blog);
			$dom = domxml_open_mem($blog);
			$dom = new DomDocument($blog);
			
			// archive
			$xpath = $this->GetXpathContext($dom);
			$totalNode = $xpath->xpath_eval('//openSearch:totalResults');
			$total = $dom->dump_node($totalNode->nodeset[0]->first_child());

			$xpath = $this->GetXpathContext($dom);
			$startNode = $xpath->xpath_eval('//openSearch:startIndex');
			$start = $dom->dump_node($startNode->nodeset[0]->first_child());

			$xpath = $this->GetXpathContext($dom);
			$countNode = $xpath->xpath_eval('//openSearch:itemsPerPage');
			$count = $dom->dump_node($countNode->nodeset[0]->first_child());
			
			// posts
			$nodes = $xpath->xpath_eval('//entry');
			$posts = $nodes->nodeset;

			$this->output = '';
			foreach ($posts as $key=>$post)
			{
				$title = $this->GetElementValue($post, 'title');
				$id = $this->GetElementValue($post, 'id');
				$published = $this->GetElementValue($post, 'published');
				$summary = $this->GetElementValue($post, 'summary');
				$reactiesNode = $this->GetXpath($post, '/link[@rel="replies" and @type="text/html"]');
//print('<pre>'.var_export($post, true).'</pre>');exit;

				if (strpos($id, 'summary/'))
				{
					preg_match_all('|.*summary/(?P<id>\d*)|', $id, $id, PREG_SET_ORDER);
				}
				if (strpos($id, 'posts/'))
				{
					preg_match_all('|.*posts/(?P<id>\d*)|', $id, $id, PREG_SET_ORDER);
				}
				//$id = $id[0]['id'];
	            $id = substr($id, strpos($id, '.post-') + 6 );
				$published = mktime(substr($published,11,2),
				                    substr($published,14,2),
				                    substr($published,17,2),
				                    substr($published,5,2),
				                    substr($published,8,2),
				                    substr($published,0,4));

				$this->output .= '<h3><a href="/blog/'.$id.'">' . htmlspecialchars($title) . '</a></h3>';
				$this->output .= '<span style="font-style:italic;font-size:80%;">' . date('d-m-Y H:i:s', $published) . '</span>';
				$this->output .= '<p>';
				$this->output .= str_replace('--NL--', '<br />', $summary) . ' ...';
				$this->output .= '</p>';
				$this->output .= '<p><a href="'.htmlspecialchars($reactiesNode->nodeset[0]->get_attribute('href')).'">'.$reactiesNode->nodeset[0]->get_attribute('title').'</a></p>';
				$this->output .= '<p style="font-style:italic;">Lees <a href="/blog/'.$id.'">' . htmlspecialchars($title) . '</a></p>';
			}

			// Vorige - Volgende
			if ($start > 1)
			{
				$this->output .= '<a style="float:left;clear:none;" href="/blog/page/'. ((($start-$count) >= 1) ? ((($start-$count) < $total) ? ($start-$count) : ($total-$count+1)) : 1 ) . '">Vorige</a> ';
			}
			if (($start+$count) < $total)
			{
				$this->output .= '<a style="float:right;clear:none;" href="/blog/page/'. ($start+$count) . '">Volgende</a>';
			}
			$this->output .= '<br style="clear:both;" />';
		}
	}
	
	function PrintPost(&$posts, $comments, $blogID, $postID, $error)
	{
		// Posts
		if ($error[0])
		{
			$this->PrintError($error);
		}
		else
		{
			$posts = preg_replace("|\s*xmlns=['\"][^'\"]*['\"]|", "", $posts);
			$posts = utf8_encode($posts);
			$dom = domxml_open_mem($posts);
//print('<pre>'.var_export($posts, true).'</pre>');exit;
			
			$xpathContext = xpath_new_context($dom);
			xpath_register_ns($xpathContext, '', 'http://www.w3.org/2005/Atom');
			
			$title = $this->GetNodeValue($xpathContext, '/entry/title');
			$published = $this->GetNodeValue($xpathContext, '/entry/published');
			$postLink = $this->GetNodeValue($xpathContext, '/entry/link[@rel="alternate"]/@href');
			$content = $this->GetNodeValue($xpathContext, '/entry/content');
			//$content = $this->GetNodeContent($dom, $xpathContext, '/entry/content');

			$published = mktime(substr($published,11,2),
								substr($published,14,2),
								substr($published,17,2),
								substr($published,5,2),
								substr($published,8,2),
								substr($published,0,4));

			$this->output  = '';
			$this->output .= '<h2>' . htmlspecialchars($title) . '</h2>';
			$this->output .= '<p style="font-style:italic;font-size:80%;">' . date('d-m-Y H:i:s', $published) . '</p>';
			$this->output .= $content;
			
			// Comments
			$comments = preg_replace("|\s*xmlns=['\"][^'\"]*['\"]|", "", $comments);
//		$comments = utf8_encode($comments);
			$dom = domxml_open_mem($comments);
	   
			$xpathContext = xpath_new_context($dom);
			xpath_register_ns($xpathContext, '', 'http://www.w3.org/2005/Atom');
			
			$nodes = $xpathContext->xpath_eval('//entry');
			$comm = $nodes->nodeset;

			$this->output .= '<p style="font-weight:bold;">';
			if (count($comm) > 0)
			{
				$this->output .= count($comm) . ' ' . (count($comm) > 1 ? 'reacties' : 'reactie') . ':';
			}
			else
			{
				$this->output .= 'Geen reacties';
			}
			$this->output .= '</p>';

			$nodes = $xpathContext->xpath_eval('//entry');
			$comm = $nodes->nodeset;
			foreach ($comm as $key=>$comment)
			{
				$content = $this->GetElementValue($comment, 'content');
				$author = $this->GetElementValue($comment, 'name');
				$published = $this->GetElementValue($comment, 'published');

				$published = mktime(substr($published,11,2),
									substr($published,14,2),
									substr($published,17,2),
									substr($published,5,2),
									substr($published,8,2),
									substr($published,0,4));

				$this->output .= '<p style="font-weight:bold">' . $author . ':</p>';
				$this->output .= '<p style="padding-left:50px">' . str_replace('<BR/>','<br />',$content) . '<br /><br />';
				$this->output .= '<span style="font-style:italic;font-size:80%;">(' . date('d-m-Y H:i:s', $published) . ')</span></p>';
			}

			$this->output .= '<p style="font-style:italic;"><a href="http://www.blogger.com/comment.g?blogID='.$blogID.'&amp;postID='.$postID.'">Plaats uw reactie</a></p>';
			//$this->output .= '<p style="font-style:italic;"><a href="'.$postLink.'#comments">Toon alle reacties</a></p>';

			$this->output = utf8_decode($this->output);
		}
	}
	
	function PrintError($error)
	{
		$this->output = '<span class="error">Er is een fout opgetreden in de blog: ['.$error[0].'] '.$error[1].'</span>';
	}
	
	/* XML Functions */
	function GetXpathContext($dom)
	{
//print('<pre>'.var_export($dom, true).'</pre>');exit;
		$xpath = xpath_new_context($dom);
		xpath_register_ns($xpath, '', 'http://www.w3.org/2005/Atom');
		xpath_register_ns($xpath, 'openSearch', 'http://a9.com/-/spec/opensearchrss/1.0/');
		
		return $xpath;
	}
	
	function GetElementValue($node, $elementName)
	{
		$elements = $node->get_elements_by_tagname($elementName);
		$element = $elements[0];
		$value = $element->get_content();
		
		return $value;
	}
	
	function GetXpath($node, $xpath)
	{
		$xpathContext = xpath_new_context($node);
		
		$elements = $xpathContext->xpath_eval($xpath);
//print('<pre>'.var_export($elements, true).'</pre>');
		return $elements;
	}
	
	function GetNodeValue($xpathContext, $xpath)
	{
		$nodes = $xpathContext->xpath_eval($xpath);
		$node = $nodes->nodeset[0]->first_child();
		$value = $node->node_value();
		
		return $value;
	}
	
	function GetNodeContent($dom, $xpathContext, $xpath)
	{
		$nodes = $xpathContext->xpath_eval($xpath);
		$node = $nodes->nodeset[0]->first_child();
		$value = $dom->dump_node($node);
		
		return $value;
	}
}
?>